package pantallas;
import gestores.GestorPedidos;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ListModel;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;

import negocio.Comanda;
import negocio.ItemComanda;
import negocio.Plato;
import persistencia.DAOComanda;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class PanelDeControlView extends javax.swing.JFrame {
	private JLabel lblComandasEnElaboracion;
	private JButton cmdDemorar;
	private JButton cmdPasarEnPreparacion;
	private JTree ArbolEnPreparacion;
	private JScrollPane jScrollPane1;
	private JLabel lblComandas;
	private JTree ArbolEnCurso;
	private JButton cmdSalir;
	private int tiempoAcumulado = 0;
	//RAIZ
	
	
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				PanelDeControlView inst = new PanelDeControlView();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public PanelDeControlView() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			pack();
			this.setSize(545, 608);
			setTitle("Panel de Control");
			{
				cmdSalir = new JButton();
				getContentPane().add(cmdSalir);
				cmdSalir.setText("Salir");
				cmdSalir.setBounds(442, 544, 84, 21);
				cmdSalir.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						System.out.println("cmdSalir.actionPerformed, event="+evt);
						dispose();
					}
				});
			}
			{
				lblComandas = new JLabel();
				getContentPane().add(lblComandas);
				lblComandas.setText("Comandas en espera");
				lblComandas.setBounds(14, 14, 175, 14);
			}
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				jScrollPane1.setBounds(14, 35, 168, 497);
				{
					ArbolEnCurso = new JTree(GestorPedidos.getInstance().getTreeContent("Espera"));
					jScrollPane1.setViewportView(ArbolEnCurso);
				}
			}
			{
				ArbolEnPreparacion = new JTree(GestorPedidos.getInstance().getTreeContent("Preparacion"));
				getContentPane().add(ArbolEnPreparacion);
				ArbolEnPreparacion.setBounds(364, 40, 161, 497);
			}
			{
				lblComandasEnElaboracion = new JLabel();
				getContentPane().add(lblComandasEnElaboracion);
				lblComandasEnElaboracion.setText("Comandas en Preparacion");
				lblComandasEnElaboracion.setBounds(364, 14, 154, 14);
			}
			{
				cmdPasarEnPreparacion = new JButton();
				getContentPane().add(cmdPasarEnPreparacion);
				cmdPasarEnPreparacion.setText(">");
				cmdPasarEnPreparacion.setBounds(218, 57, 103, 21);
				cmdPasarEnPreparacion.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
	
						
						
						
						System.out.println("cmdPasarEnPreparacion.actionPerformed, event="+evt);
						//CREO EL NODO TEMPORAL
						DefaultMutableTreeNode comandaAux = new DefaultMutableTreeNode();
						//PRIMERO DETECTO EL NODO Y LO GUARDO EN UN TEMPORAL.
						System.out.println("cmdPasarEnPreparacion.actionPerformed, event="+evt);
						comandaAux = (DefaultMutableTreeNode) ArbolEnCurso.getLastSelectedPathComponent();
						System.out.println("Cantidad de platos en la comanda seleccionada: "+ comandaAux.getChildCount());
						//VALIDO QUE SEA UN NODO DE COMANDA.
						if (comandaAux.getChildCount()==0){
							JOptionPane.showMessageDialog(null, "EL COMPONENTE SELECCIONADO NO ES UNA COMANDA");
						}else{
							//LO SACO DEL PRIMER ARBOL
							DefaultTreeModel model = (DefaultTreeModel)ArbolEnCurso.getModel();
							DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
							root.remove(comandaAux);
							model.reload(root);
							//LO METO EN EL SEGUNDO ARBOL
							DefaultTreeModel model2 = (DefaultTreeModel)ArbolEnPreparacion.getModel();
							DefaultMutableTreeNode root2 = (DefaultMutableTreeNode)model2.getRoot();
							String cadena = (String) comandaAux.getUserObject();
							int codigo = Integer.valueOf(cadena.substring(0, cadena.indexOf(" - ")));
							int tiempoInicial = Integer.valueOf(cadena.substring(cadena.indexOf(" - ")+3, cadena.length()));
							tiempoAcumulado+=tiempoInicial;
							String finale = String.valueOf(codigo) + " - " + String.valueOf(tiempoAcumulado);
							comandaAux.setUserObject(finale);
							root2.add(comandaAux);
							model2.reload(root2);
							String cadena2 = (String) comandaAux.getUserObject();
							GestorPedidos.getInstance().cambiaEstadoDeComandaAEnPreparacion(Integer.valueOf(cadena2.substring(0, cadena2.indexOf(" - "))));
						}
					}});}
			{
				cmdDemorar = new JButton();
				getContentPane().add(cmdDemorar);
				cmdDemorar.setText("Demorar");
				cmdDemorar.setBounds(218, 93, 109, 21);
				cmdDemorar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						DefaultMutableTreeNode comandaAuxiliar = (DefaultMutableTreeNode) ArbolEnPreparacion.getLastSelectedPathComponent();
						String cadena = (String) comandaAuxiliar.getUserObject();
						int tiempodeventana = Integer.valueOf(JOptionPane.showInputDialog("Ingrese demora"));
					
						GestorPedidos.getInstance().demorarComanda(Integer.valueOf(cadena.substring(0, cadena.indexOf(" - "))), tiempodeventana);
						int codigo = Integer.valueOf(cadena.substring(0, cadena.indexOf(" - ")));
						int tiempoInicial = Integer.valueOf(cadena.substring(cadena.indexOf(" - ")+3, cadena.length()));
						tiempoInicial+=tiempodeventana;
						String finale = String.valueOf(codigo) + " - " + String.valueOf(tiempoInicial);
						comandaAuxiliar.setUserObject(finale);
						
						DefaultTreeModel model2 = (DefaultTreeModel)ArbolEnPreparacion.getModel();
						model2.nodeChanged(comandaAuxiliar);
						System.out.println("cmdDemorar.actionPerformed, event="+evt);
			            
						DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode)ArbolEnPreparacion.getSelectionPath().getLastPathComponent();
			            DefaultTreeModel dtm = (DefaultTreeModel)ArbolEnPreparacion.getModel();    // get the tree model
			            int index = dtm.getIndexOfChild(dtm.getRoot(), dmtn);
			            System.out.println(index);
			            dtm.insertNodeInto(dmtn, (DefaultMutableTreeNode)dtm.getRoot(), index+1);
						dtm.reload();
					}
				});
			}
			//MEDIDAS Y CENTRALIZACION DE LA PANTALLA.
	        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
	        int w = this.getSize().width;
	        int h = this.getSize().height;
	        int x = (dim.width-w)/2;
	        int y = (dim.height-h)/2;
	        this.setLocation(x, y);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

}
